package com.sepro.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;

import com.sepro.bean.PaymentProduct;

public class PaymentProductUtil {

	private static final String PAYMENT_PRODUCT_XML_PATH = "shtpConfig.xml";
	
	
	public static List<PaymentProduct> getPaymentProductList(List<PaymentProduct> retList) {
		
		//List<PaymentProduct> retList = new ArrayList<PaymentProduct>();
		if(retList == null)
			retList = new ArrayList<PaymentProduct>();

		File shtpXmlFile = null;
		SAXReader saxReader = null;
		Document document = null;
		try {
			shtpXmlFile = new ClassPathResource(PAYMENT_PRODUCT_XML_PATH).getFile();
			saxReader = new SAXReader();
			document = saxReader.read(shtpXmlFile);
		} catch (DocumentException e) {
			System.out.println("read xml file exception: DocumentException!");
		} catch (IOException e) {
			System.out.println("read xml file exception: IOException!");
		}
		@SuppressWarnings("unchecked")
		List<Node> paymentProductList = document.selectNodes("/shtp/paymentProduct/product");
		Iterator<Node> iter = paymentProductList.iterator();
		while(iter.hasNext()){
			
			PaymentProduct pp = new PaymentProduct();
			Node node = iter.next();
			
			Node nameNode = node.selectSingleNode("name");
			if(nameNode != null){ pp.setName(nameNode.getText()); }
			
			Node despNode = node.selectSingleNode("description");
			if(despNode != null){ pp.setDescription(despNode.getText()); }
			
			Node bargainorIdNameNode = node.selectSingleNode("bargainorIdName");
			if(bargainorIdNameNode != null){ pp.setBargainorIdName(bargainorIdNameNode.getText()); }
			
			Node bargainorKeyNameNode = node.selectSingleNode("bargainorKeyName");
			if(bargainorKeyNameNode != null){ pp.setBargainorKeyName(bargainorKeyNameNode.getText()); }
			
			Node logoPathNode = node.selectSingleNode("logoPath");
			if(logoPathNode != null){ pp.setLogoPath(logoPathNode.getText()); }
			
			Node classNameNode = node.selectSingleNode("className");
			if(classNameNode != null){ pp.setClassName(classNameNode.getText()); }
			
			retList.add(pp);
		}
		return retList;
	}
}
